<?php
    class Plugins_Questionnaire extends model {

    
    function getformadd($productid="")
  {
  
$code  = '<div class="wrpPlg clBg5 clBd2 bdFull">'."\n";
$code .= '<div class="wrpOutAtcPlg">'."\n";
$code .= '<div class="wrpAtcPlg">'."\n";
$code .= '<span class="ico2 icoQustPlg"></span>'."\n";
$code .= '<div class="spnAtcPlg clTxtW">'."\n";
$code .= '<span class="txtAtcPlg fs03">Questionnaire</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="wrpTl">'."\n";
$code .= '<div title="Close" class="btnIco closeTl clBd3 mgBtm10 deleterowbutton">'."\n";
$code .= '<span class="ico icoCloseTl"></span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<form class="frmFrmPlg frmQustPlg clTxt3 fs03">'."\n";
$code .= '<fieldset class="wrpFrmPlg">'."\n";

$code .= '<div class="qstUsrType">'."\n";

$code .= '<table class="tbGenInfo tbAddCtnt clTxt3 fs03">'."\n";
$code .= '<tbody><tr>'."\n";
$code .= '<th>Questionnaire Title</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input name="questitle[]" type="text" class="clBg5 clBd2 fs03 clTxt2">'."\n";
$code .= '<input name="squeid[]" type="hidden" >'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>User Type</th>'."\n";
$code .= '<td>'."\n";
$code .= '<div class="userRsv">'."\n";

$code .= '<input type="radio" value="0" id="gstqst" name="userquestion">'."\n";
$code .= '<label for="gstqst">Guest</label>'."\n";
$code .= '<input type="radio" value="1" id="mbqst" name="userquestion">'."\n";
$code .= '<label for="mbqst">Member</label>'."\n";
$code .= '<input type="radio" value="3" id="pmuserqst" name="userquestion">'."\n";
$code .= '<label for="pmuserqst">Member Plus</label>'."\n";
$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '</tbody></table>'."\n";

$code .= '</div>'."\n";



$code .= '<div class="bntAddQuest">'."\n";
$code .= '<input type="button" name="morequestion" value="Add More Question" class="morequestion btnGen clTxtW cutTxt clBgSp2 fs04">'."\n";
$code .= '</div>'."\n";
$code .= '</fieldset>'."\n";
$code .= '</form>'."\n";
$code .= '</div>';

$arraydata['form']=addslashes(str_replace(array("\r", "\n", "\r"), '', $code));

return $arraydata;
  
  }
  
  
  function getplugin($plugreid)
  {
  
 
     $sql="select * from tb_plugins_questionnaire where plugreid=".$plugreid;
     $arrayplugin=$this->db->db_set_1recordrow($sql);
     
     $sql='select * from tb_plugins_questionnaire_form where squeid='.$arrayplugin['squeid'];
     
     $arrayform=$this->db->db_set_recordset($sql);
     $k=0;
     if(count($arrayform))
     {
     foreach($arrayform as $valueform)
     {
     
     
     $sql='select * from tb_plugins_questionnaire_subform where questid='.$valueform['questid'];
     $arraydataset=$this->db->db_set_recordset($sql);
     $arrayform[$k]['datarow']=$arraydataset;
     $k++;
     
     
     }
     
     }
     
     
     $arrayplugin['form']=$arrayform;
     
//     echo "<pre>";
//     print_r($arrayplugin);
//     echo "</pre>;";
//     exit;
     

  
     return $arrayplugin;

  
  
  

  
  }
  
   function getformlanding($arraydata)
   {
   

   
   $code  = '<form id="formsqueid-'.$arraydata['squeid'].'" name="submitformdata" ><div class="ldIt wrpQust clBd2 bdFull clBg2 squeidclass" >'."\n";
   
   if($this->userdata['userid'])
   {
   $mypost=$this->checkmyproductbyuserid($arraydata['productid'],$this->userdata['userid']);
   if($mypost)
   {
$code .= '<div class="expQust" onclick="exportdatareport('.$arraydata['productid'].','.$arraydata['squeid'].','.$arraydata['plugreid'].')">'."\n";
$code .= '<input type="button" class="btnEPRsv clBgBt1" value="">'."\n";
$code .= '</div>'."\n";
   }
   }



$code .= '<div class="ttlQst">'."\n";
$code .= '<span class="clTxt2 fs04 boldGen">'.$arraydata['title'].'</span>'."\n";
$code .= '</div>'."\n";

$code .='<input type="hidden" value="'.$arraydata['squeid'].'" name="squeid" >';
$code .='<input type="hidden" value="'.$arraydata['productid'].'" name="productid" >';
//$code .='<input type="hidden" value="'.$arraydata['squeid'].'" name="squeid" class="clBg5 clBd2 fs03 clTxt3">';



$krow=1;
if(count($arraydata['form']))
{
foreach($arraydata['form'] as $valueform)
{
$code .= '<div class="ldIt wrpFrmLd clBg4">'."\n";
$code .= '<div class="wrpHdInfoGen">'."\n";
$code .= '<div class="wrpInFrmLd">'."\n";
$code .= '<h2 class="boldGen clTxt3 fs04">'.$krow.'. '.$valueform['title'].'</h2>'."\n";
$code .= '<div class="wrpFrmInLd">'."\n";







switch (intval($valueform['typeid'])) {
    case 1:
        if(count($valueform['datarow']))
        {
        foreach($valueform['datarow'] as $valuerow)
        {
        
        $code .= '<div class="itFrmLd radioFrmLd">'."\n";
        $code .= '<input type="radio" value="'.$valuerow['squestid'].'" name="answer['.($krow-1).']" >'."\n";
        if($valuerow['etc'])
        {
        
        $code .= '<label for="rdoOpt1" class="clTxt3 fs03">อื่นๆ โปรดระบุ</label><input type="text" name="etctext['.$valuerow['squestid'].']" class="textother">'."\n";
        }else
        {
        $code .= '<label for="rdoOpt1" class="clTxt3 fs03">'.$valuerow['value'].'</label>'."\n";
        
        }
        
        
        
        $code .= '</div>';
        
        
        
        }
        
        
        $code .= '<input type="hidden" name="questid[]" value="'.$valueform['questid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
        $code .= '<input type="hidden" name="typeid[]" value="'.$valueform['typeid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
        }
        break;
    case 2:
        if(count($valueform['datarow']))
        {
        foreach($valueform['datarow'] as $valuerow)
        {
        
        $code .= '<div class="itFrmLd radioFrmLd">'."\n";
        $code .= '<input type="checkbox" value="'.$valuerow['squestid'].'" name="answer['.($krow-1).'][]" >'."\n";
        if($valuerow['etc'])
        {
        
        $code .= '<label for="rdoOpt1" class="clTxt3 fs03">อื่นๆ โปรดระบุ</label><input type="text" name="etctext['.$valuerow['squestid'].']" class="textother">'."\n";
        }else
        {
        $code .= '<label for="rdoOpt1" class="clTxt3 fs03">'.$valuerow['value'].'</label>'."\n";
        
        }
        
        
        
        $code .= '</div>';
        
        
        
        }
        
        
        $code .= '<input type="hidden" name="questid[]" value="'.$valueform['questid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
        $code .= '<input type="hidden" name="typeid[]" value="'.$valueform['typeid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
        }
        break;
    case 3:
    $code .= '<div class="itFrmLd inpTxtFrmLd">'."\n";
    $code .= '<input type="text" name="answer['.($krow-1).']" class="clBg5 clBd2 fs03 clTxt3">'."\n";
    $code .= '<input type="hidden" name="questid[]" value="'.$valueform['questid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
    $code .= '<input type="hidden" name="typeid[]" value="'.$valueform['typeid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
    $code .= '</div>'."\n";
        break;
    case 4:
    
     $code .= '<div class="itFrmLd txtAreaFrmLd">'."\n";
     $code .= '<textarea name="answer['.($krow-1).']" class="clBg5 clBd2 fs03 clTxt3"></textarea>'."\n";
     $code .= '<input type="hidden" name="questid[]" value="'.$valueform['questid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
     $code .= '<input type="hidden" name="typeid[]" value="'.$valueform['typeid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
     $code .= '</div>';

        break;
    case 5:
        if(count($valueform['datarow']))
        {
        $code .= '<div class="itFrmLd dropDownFrmLd"><div class="reDesign"><select class="fs01" name="answer['.($krow-1).']">'."\n";
        $code .='<option value="0">please select answer</option>';
        foreach($valueform['datarow'] as $valuerow)
        {
        
        
            $code .='<option value="'.$valuerow['squestid'].'">'.$valuerow['value'].'</option>';
        
        
        
        
        
        
        }
        $code .= '</select></div></div>';
        
        
        $code .= '<input type="hidden" name="questid[]" value="'.$valueform['questid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
        $code .= '<input type="hidden" name="typeid[]" value="'.$valueform['typeid'].'" class="clBg5 clBd2 fs03 clTxt3">'."\n";
        }
        break;
}


$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>';
$krow++;
}

}



$code .= '<div class="ldIt wrpBtnLd clBg4">'."\n";
$code .= '<div class="wrpBtnQst">'."\n";
$code .= '<div class="booking clBgSp2">'."\n";
$code .= '<span class="clTxtW fs09" onclick="submitanswerform('.$arraydata['squeid'].')">SEND</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";

$code .= '</div></form>';


$data['str']=$code;
$data['target']='secondtarget';
  
  return $data;
   
   
   }

  
    function saveplugin($arraydata,$productid,$plugreid)
  {
  

  $arraydata2=$arraydata;
  $arraydata['productid']=$productid;
  $arraydata['plugreid']=$plugreid;
  
  unset($arraydata['datarowobj']);
  unset($arraydata['deleterowobj']);
  unset($arraydata['deleterowobj2']);
  
  if($arraydata['squeid'])
  {
  $squeid=$arraydata['squeid'];
  
  }else
  {
    $sql='SELECT MAX(squeid) as squeid from tb_plugins_questionnaire';
    $arrayspoll=$this->db->db_set_1recordrow($sql);
    $arrayspoll['squeid']=intval($arrayspoll['squeid']);
     $squeid=$arrayspoll['squeid']+1;
  
  }
  $arraydata['squeid']=$squeid;
  
  $this->db->db_set($arraydata,'tb_plugins_questionnaire');
  
  
  if(count($arraydata2['datarowobj']))
  {
  foreach($arraydata2['datarowobj'] as $valuerow)
  {
  
  $arraydata3=array();
  $arraydata3['typeid']=$valuerow['typeid'];
  $arraydata3['title']=$valuerow['title'];
  $arraydata3['squeid']=$squeid;
  $arraydata3['productid']=$productid;
  

  if($valuerow['questid'])
  {
  $questid=$valuerow['questid'];
  $this->db->db_set($arraydata3,'tb_plugins_questionnaire_form',' questid='.$valuerow['questid']);
  
  
  }else
  {
  
  $this->db->db_set($arraydata3,'tb_plugins_questionnaire_form');
  $questid=$this->db->db_get_last_number();
  
  }
  
  
  if(count($valuerow['datarow']))
  {
  foreach($valuerow['datarow'] as $valuedatarow)
  {
  $arraydata4=array();
  $arraydata4['questid']=$questid;
  $arraydata4['squeid']=$squeid;
  $arraydata4['productid']=$productid;
  $arraydata4['value']=$valuedatarow['value'];
  $arraydata4['etc']=$valuedatarow['etc'];

  
  
  if($valuedatarow['squestid'])
  {
  //$questid=$valuerow['questid'];
  $this->db->db_set($arraydata4,'tb_plugins_questionnaire_subform',' squestid='.$valuedatarow['squestid']);
  
  
  }else
  {
  
  $this->db->db_set($arraydata4,'tb_plugins_questionnaire_subform');
  //$questid=$this->db->db_get_last_number();
  
  }
  
  
  }
  }
  
  
  }
  
  }
  

    if(count($arraydata2['deleterowobj']))
    {
    foreach($arraydata2['deleterowobj'] as $valuerow)
  {
  if($valuerow)
  {
  $sql="delete from tb_plugins_questionnaire_form where questid=".$valuerow;
  $this->db->db_delete($sql);
  
  
  
  $sql="delete from tb_plugins_questionnaire_subform where questid=".$valuerow;
  $this->db->db_delete($sql);
  
  
  
  $sql="delete from tb_plugins_questionnaire_value where questid=".$valuerow;
  $this->db->db_delete($sql);
  
  
  
  }
  
  
  
  }
    
    
    
    }
    
    
    
    
    
  if(count($arraydata2['deleterowobj2']))
    {
    foreach($arraydata2['deleterowobj2'] as $valuerow)
  {
  if($valuerow)
  {
  
  
  
  
  $sql="delete from tb_plugins_questionnaire_subform where squestid=".$valuerow;
  $this->db->db_delete($sql);
  
  
  
    $sql="delete from tb_plugins_questionnaire_value where squestid=".$valuerow;
  $this->db->db_delete($sql);
  
  
  

  
  
  
  }
  
  
  
  }
    
    
    
    }
    
    

  
  
  
  
  }
  
     function submitanswerform()
     {
     
     $arraydata['data']=array();
     $arraydataset=array();
     if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   
   }
     
    if($this->post['productid']) 
    {
        $sql="select * from tb_plugins_questionnaire where productid=".$this->post['productid'];
        $arrayplugins=$this->db->db_set_1recordrow($sql);
     
     
        if($arrayplugins['userquestion'])
   {
   
   if($_COOKIE['oauth'])
   {
  // $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   
   if(intval($userdata['roleid'])>=intval($arrayplugins['userquestion']))
   {
   
   
   }else
   {
   $arraydata['error'][]=33;
   return $arraydata;
   }
   
   }else
   {
   $arraydata['error'][]=5;
   return $arraydata;
   }
   
   }
     
     
     if(count($this->post['answer']))
     {
     $sql='SELECT MAX(maxrow) as maxrow from tb_plugins_questionnaire_value';
     $arraymaxrow=$this->db->db_set_1recordrow($sql);
     $arraymaxrow['maxrow']=intval($arraymaxrow['maxrow']);
     $maxrow=$arraymaxrow['maxrow']+1;
     
     
     $krow=0;
     foreach($this->post['answer'] as $valueanswer)
     {
     $noinsert=1;
     $arraydataset=array();
     $arraydataset['productid']=$this->post['productid'];
     $arraydataset['questid']=$this->post['questid'][$krow];
     $arraydataset['squeid']=$this->post['squeid'];
     $arraydataset['maxrow']=$maxrow;
     
     
     
     
     $arraydataset['typeid']=intval($this->post['typeid'][$krow]);
     if(!is_array($valueanswer))$arraydataset['value']=$valueanswer;
     
     
     
   $arraydataset['ip']=$_SERVER["REMOTE_ADDR"];
   if($userdata['userid'])
   {
   $arraydataset['createby']=$userdata['userid'];
   }else
   {
   $arraydataset['createby']=0;
   
   }
   
   $arraydataset['createdate']=date("Y-m-d H:i:s");
   

    if($arraydataset['typeid']==1)
    {
    $arraydataset['squestid']=intval($valueanswer);
    
    if($this->post['etctext'][$valueanswer])
    {
    $arraydataset['value']=$this->post['etctext'][$valueanswer];
    }
    
    
    
    }else if($arraydataset['typeid']==2)
    {
    
    $noinsert=0;
    if(is_array($valueanswer)&&count($valueanswer))
    {
    foreach($valueanswer as $valueanswer2)
    {
    
    $arraydataset['squestid']=$valueanswer2;
    
    if($this->post['etctext'][$valueanswer2])
    {
    $arraydataset['value']=$this->post['etctext'][$valueanswer2];
    }
    
    
    $this->db->db_set($arraydataset,'tb_plugins_questionnaire_value');
    
    }
    
    }
    
    
    }
    else if($arraydataset['typeid']==5)
    {
    
    $arraydataset['squestid']=intval($valueanswer);
    
    }
    
    else
    {
    $arraydataset['squestid']=0;
    
    }
    
    
    if($noinsert)
    {
    $this->db->db_set($arraydataset,'tb_plugins_questionnaire_value');
    
    }
   
     
     
     
     
     
     $krow++;
     
     
     
     }
     
     
     
     }else
     {
     
     $arraydata['error'][]=4;
      return $arraydata;
     }
     
    }

   //  $arraydata['data']=$_POST;
     
     
     return $arraydata;
     
     }
     
     function exportdatareport($productid,$squeid,$plugreid)
     {
     if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   
   
   

   

   
   if($booleancheck||$userdata['roleid']==3)
   {    
      header("Content-Type: application/vnd.ms-excel");
          header('Content-Disposition: attachment; filename="'.time().'-'.rand(0,10000).'.xls"');
   $getdata=$this->getplugin($plugreid);
   
   $sql='SELECT
tb_user.username,
tb_plugins_questionnaire_value.quevid,
tb_plugins_questionnaire_value.maxrow,
tb_plugins_questionnaire_value.productid,
tb_plugins_questionnaire_value.squeid,
tb_plugins_questionnaire_value.questid,
tb_plugins_questionnaire_value.squestid,
tb_plugins_questionnaire_value.typeid,
tb_plugins_questionnaire_value.`value`,
tb_plugins_questionnaire_value.ip,
tb_plugins_questionnaire_value.createby,
tb_plugins_questionnaire_value.createdate,
tb_plugins_questionnaire_subform.`value` AS subvalue,
tb_plugins_questionnaire_subform.etc
FROM
tb_plugins_questionnaire_value
INNER JOIN tb_user ON tb_plugins_questionnaire_value.createby = tb_user.userid
LEFT JOIN tb_plugins_questionnaire_subform ON tb_plugins_questionnaire_value.squestid = tb_plugins_questionnaire_subform.squestid
where tb_plugins_questionnaire_value.squeid='.$getdata['squeid'].' order by tb_plugins_questionnaire_value.createdate asc';
$arrayvalue=$this->db->db_set_recordset($sql);
$arraynewvalue=array();
$krow=1;
if(count($arrayvalue))
{
foreach($arrayvalue as $valuedata)
{

if(!isset($arraynewvalue[$valuedata['maxrow']]))
{
$arraynewvalue[$valuedata['maxrow']]['no']=$krow;
$arraynewvalue[$valuedata['maxrow']]['username']=$valuedata['username'];






$krow++;
}

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['questid']=$valuedata['questid'];
if($valuedata['typeid']=="1")
{
if($valuedata['etc']=='1')
{

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['value']=$valuedata['value'];
}else
{

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['value']=$valuedata['subvalue'];


}


}
else if($valuedata['typeid']=="2")
{
if($valuedata['etc']=='1')
{

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['value'][]=$valuedata['value'];
}else
{

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['value'][]=$valuedata['subvalue'];


}


}
else if($valuedata['typeid']=="5")
{

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['value']=$valuedata['subvalue'];

}
else
{

$arraynewvalue[$valuedata['maxrow']]['data'][$valuedata['questid']]['value']=$valuedata['value'];

}



}

}




//echo "<pre>";
//print_r($arraynewvalue);
//echo "</pre>;";
//exit;
   ?>
<html xmlns:o="urn:schemas-microsoft-com:office:office"

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns="http://www.w3.org/TR/REC-html40">


<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</HEAD><BODY>

<TABLE  x:str BORDER="1">

   
   
   <tr align="center" >
   
    <td colspan="<?echo count($getdata['form'])+2;?>"><? echo $getdata['title'];?></td>
    
  </tr>
   
   <tr align="center">
    <td>No</td>
    <td>Username</td>
    <?if(count($getdata['form'])){
    foreach($getdata['form'] as $valueform)
    {
     echo '<td>'.$valueform['title'].'</td>';
    }
    }
    
    ?>
  </tr>
  
   <?if(count($arraynewvalue)){
   foreach($arraynewvalue as $valueformdata)
   {
   ?>
  <tr align="center">
    <td><?=$valueformdata['no']?></td>
    <td><?=$valueformdata['username']?></td>
    
    <?if(count($getdata['form'])){
    foreach($getdata['form'] as $valueform)
    {
      if($valueformdata['data'][$valueform['questid']])
      {
      if(is_array($valueformdata['data'][$valueform['questid']]['value']))
      {
      echo '<td>'.join(",",$valueformdata['data'][$valueform['questid']]['value']).'</td>';
      
      }else
      {
      echo '<td>'.$valueformdata['data'][$valueform['questid']]['value'].'</td>';
      
      }
      }else
      {
      
      echo '<td>&nbsp</td>';
      
      }
     
     
     
    }
    }?>
    
  
  </tr>
  
  <?
   }
  }?>



  </TABLE>
  
  </BODY>

  </HTML>
   
   <?
   
   
   }
   
   
   
   
   
   }
     
     }
  
  
    }
?>
